﻿using Autofac;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using ZingBlog.Web.Areas.Admin.Controllers;
using Module = Autofac.Module;

namespace ZingBlog.Web
{
    public  class AutofacDefaultModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var iRepository = Assembly.Load("ZingBlog.Domain");
            var repository = Assembly.LoadFile($"{AppContext.BaseDirectory}ZingBlog.Repository.dll");
            builder.RegisterAssemblyTypes(iRepository, repository)
             .Where(t => t.Name.EndsWith("Repository"))
             .AsImplementedInterfaces().PropertiesAutowired();

            var iserviceTypes = Assembly.Load("ZingBlog.IService");
            var serviceTypes = Assembly.LoadFile($"{AppContext.BaseDirectory}ZingBlog.Service.dll");
            builder
                .RegisterAssemblyTypes(iserviceTypes, serviceTypes)
                .Where(t => t.Name.EndsWith("Service"))
                .AsImplementedInterfaces()
                .PropertiesAutowired()
                .InstancePerLifetimeScope(); ;

            var controllerBaseType = typeof(ControllerBase);
            builder.RegisterAssemblyTypes(typeof(Program).Assembly)
                .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
                .PropertiesAutowired();
        }
           
    }
}
